package com.encryption.logic;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.encryption.logic.impl.FilePermissionParser;
import com.encryption.model.FilePermissionConf;
import com.encryption.service.qrc.DocProcessParam;
import com.encryption.service.qrc.DocProcessService;
import com.encryption.service.qrc.impl.DocProcessServiceImpl;
import com.log4ic.DocViewer;
import com.log4ic.utils.convert.office.OfficeConverter;
import com.log4ic.utils.convert.pdf.PDFConverter;
import com.log4ic.utils.io.FileEncryption;
import com.log4ic.utils.io.FileUtils;

public class DocOperationHelper {
	private static ExecutorService executorService = Executors
			.newFixedThreadPool(5);
	private static DocProcessService docProcessService = new DocProcessServiceImpl();

	public static String converter(String fileName) {
		return converter(fileName, "", "");
	}

	public static String converter(String fileName, String inputPath,
			String outputPath) {
		if ("".equals(outputPath))
			outputPath = DocViewer.getOutputPath();
		if ("".equals(inputPath))
			inputPath = DocViewer.getInputPath();
		String suffix = FileUtils.getFileSuffix(fileName);
		File sourceFile = new File(inputPath + "/" + fileName);
		boolean deletePdf = true;
		File pdfFile = null;
		if (!"pdf".equalsIgnoreCase(suffix)) {
			OfficeConverter converter = new OfficeConverter();
			File targetFile = new File(outputPath + "/" + fileName + ".pdf");

			try {
				converter.convert(sourceFile, targetFile);
				pdfFile = targetFile;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			pdfFile = sourceFile;
			deletePdf = false;
		}

		PDFConverter pdfConverter = new PDFConverter();
		try {
			String targetFile = pdfConverter.convert(pdfFile, outputPath);
//			if (deletePdf)
//				pdfFile.delete();

			return targetFile;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void addWaterMark(FilePermissionConf conf,
			com.encryption.model.File file) {
		DocProcessParam docProcessParam = FilePermissionParser
				.parse(conf, file);
		docProcessParam.setSourceFilePath(FilePermissionParser.FILE_PATH
				+ file.getFileUrl());
		docProcessParam.setTargetFilePath(FilePermissionParser.FILE_PATH
				+ file.getFileUrl());
		docProcessService.addWarterMark(docProcessParam);
	}

	public static void coryDoc(DocProcessParam param) {
		String targetPath = param.getTargetFilePath();
		File targetFile = new File(targetPath);
		if (targetFile.exists())
			return;

		File srcFile = new File(param.getSourceFilePath());
		try {
			FileUtils.coryFile(srcFile, targetFile);
		} catch (Exception e) {
		}
	}

	public static void addWaterMark(DocProcessParam docProcessParam) {
		docProcessService.addWarterMark(docProcessParam);
	}

	public static void encryptFile(final FilePermissionConf conf,
			final com.encryption.model.File file) {
		executorService.execute(new Runnable() {
			public void run() {
				docProcessService.process(FilePermissionParser
						.parse(conf, file));
			}
		});
	}
	
	public static void encryptFile(final FilePermissionConf conf,
			final com.encryption.model.File file, final boolean encrytionSave) {
		executorService.execute(new Runnable() {
			public void run() {
				docProcessService.process(FilePermissionParser
						.parse(conf, file));
				if(encrytionSave){
					String filePath = FilePermissionParser.FILE_PATH + file.getSwfUrl();
					try {
						FileEncryption.encrypt(filePath, filePath + ".sss");
						File file = new File(filePath);
						file.delete();
					} catch (Exception e) {
					}
				}
			}
		});
	}

	public static void deleteDoc(final String file) {
		executorService.execute(new Runnable() {
			public void run() {
				String srcFile = file;
				deleteFile(srcFile + ".sss");
				String pdfFile = srcFile;
				if (!srcFile.endsWith(".pdf")) {
					pdfFile = srcFile + ".pdf";
					deleteFile(pdfFile);
				}
				String swfFile = pdfFile + ".swf";
				String secFile = swfFile + ".sec";

				deleteFile(swfFile);
				deleteFile(secFile);
			}
		});
	}

	public static void deleteFile(String fileName) {
		File file = new File(fileName);
		if (file.exists())
			file.delete();
	}
	
	public static void deleteTempFile(String fileName, String dir){
		File dirFile = new File(dir);
		deleteFile(dirFile, fileName);
	}
	
	private static void deleteFile(File file, String fileName){
		if(file.getName().endsWith(".sss") || file.getName().endsWith(".swf"))
			return;
		
		if(!file.exists())
			return;
		
		if(file.isDirectory()){
			File[] fileList = file.listFiles();
			for (File f : fileList)
				deleteFile(f, fileName);
		}else{
			if(file.getName().startsWith(fileName))
				file.delete();
		}
	}
}
